-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Unit Tests For SSL Operations #1845
base: BABEL_3_X_DEV
Are you sure you want to change the base?
Adding Unit Tests For SSL Operations #1845
Conversation
Signed-off-by: Vikash Prajapati <[email protected]>
Signed-off-by: Vikash Prajapati <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be running these new tests as part of github actions. Currently I think that is not happening
Signed-off-by: Vikash Prajapati <[email protected]>
|
||
obtained = test_ssl_handshake_read(h, buf, expected, mock_socket_read, ReadPointer); | ||
|
||
size_buf = strlen(buf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strlen only works in string is null terminated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in the latest commit.
*/ | ||
if(expected == obtained) | ||
{ | ||
TEST_ASSERT_TESTCASE(*((unsigned char*)expected_str) == *((unsigned char*)buf), testResult); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we only comparing first character?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in the latest commit with complete string comparision.
snprintf(expected_str, MAX_TEST_MESSAGE_LENGTH, "%d", expected); | ||
snprintf(obtained_str, MAX_TEST_MESSAGE_LENGTH, "%d", obtained); | ||
|
||
TEST_ASSERT_TESTCASE(expected != obtained, testResult); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not clear, what do we expect here that multi packet message should be handled properly, if yes why results should not match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the logic in the latest commit.
*/ | ||
|
||
int res; | ||
tds_secure_raw_write = mock_socket_write; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we reset it after the unit testing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, have updated in the next revision.
|
||
int res; | ||
tds_secure_raw_write = mock_socket_write; | ||
unit_testing = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if there is an error during testing and unit_testing remains true? I think not an issue in current set of tests but could be a problem later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handled this using PG_TRY() and PG_FINALLY() block in the latest commit.
Signed-off-by: Vikash Prajapati <[email protected]>
(errcode(ERRCODE_ADMIN_SHUTDOWN), | ||
errmsg("terminating connection due to unexpected ssl packet header"))); | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary white spaces
tds_secure_raw_read = mock_socket_read; | ||
unit_testing = true; | ||
pkt_bytes_read = ReadPointer; | ||
PG_TRY(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
} | ||
PG_END_TRY(); | ||
|
||
tds_secure_raw_read = secure_raw_read; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put this also in finally block
tds_secure_raw_write = mock_socket_write; | ||
unit_testing = true; | ||
|
||
PG_TRY(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation, also put write API reset in finally block
|
||
obtained = test_ssl_handshake_read(h, buf, expected, mock_socket_read, ReadPointer); | ||
|
||
null_terminated_expected = strdup(expected_str); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why these copies? we can compare expected_str and buf directly. However, is buf guaranteed to be null terminated?
memcpy(obtained_str, buf, obtained); | ||
obtained_next = test_ssl_handshake_read(h, buf, expected - obtained, mock_socket_read, ReadPointer); | ||
memcpy(obtained_str + obtained, buf, obtained_next); | ||
null_terminated_expected = strdup(expected_str); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again why this copy? also if obtained_str is not null terminated how will strdup will work. You just need to put at '\0' at the end
TestResult* testResult = palloc0(sizeof(TestResult)); | ||
testResult->result = true; | ||
|
||
prelogin_request = strdup("1201000F0000010011A25E4571"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain the message format? how does it force two packet reads?
|
||
h = BIO_new(BIO_s_mem()); | ||
|
||
prelogin = malloc(strlen(buf)/2 + 8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we writing buf/2 rather than buf?
snprintf(obtained_str, MAX_TEST_MESSAGE_LENGTH, "%d", obtained); | ||
|
||
TEST_ASSERT_TESTCASE(expected == obtained, testResult); | ||
if(testResult->result == true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why both if/else cases?
expected = -1; | ||
obtained = test_ssl_handshake_write(h, buf, expected, mock_socket_write); | ||
|
||
snprintf(expected_str, MAX_TEST_MESSAGE_LENGTH, "%d", expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are not used anywhere?
Signed-off-by: Vikash Prajapati <[email protected]>
Signed-off-by: Vikash Prajapati <[email protected]>
Signed-off-by: Vikash Prajapati <[email protected]>
Signed-off-by: Vikash Prajapati <[email protected]>
Signed-off-by: Vikash Prajapati <[email protected]>
} | ||
PG_END_TRY(); | ||
|
||
return res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
TestResult* testResult = palloc0(sizeof(TestResult)); | ||
testResult->result = true; | ||
|
||
prelogin_request = strdup("1201000B00000100115461A23E"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, why are we manipulating length here?
|
||
obtained_str = (char *)malloc(obtained + 1); | ||
strncpy(obtained_str, buf, obtained); | ||
obtained_str[obtained] = '\0'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are we achieving with this comparison? both expected and obtained strings are copy of buf only. We should compared expected against prelogin string where mock socket write happens (after skipping header part)
Description
SSL (Secure Sockets Layer) is a cryptographic protocol used to establish secure connections between clients and servers. Testing SSL functionality ensures the secure transmission of data between clients and servers. Adding tests for SSL involves evaluating how the Babelfish codebase handles SSL connections.
We are keen on conducting thorough unit testing of the SslHandshake process that occurs during the prelogin stage. Our unit testing approach involves meticulously examining each step of the handshake, simulating various scenarios, and verifying the correct implementation of the SSL handshake. Through this comprehensive testing, we aim to identify any potential vulnerabilities that may exist.
To execute SSL-related tests, it is essential to enable the SSL flag during the build process using the --with-openssl command. This ensures that the necessary components for SSL testing are included. In the event that the SSL flag is not enabled, only non-SSL tests will be executed.
Authored-by: Vikash Prajapati [email protected]
Signed-off-by: Vikash Prajapati [email protected]
Test Scenarios Covered
Check List
By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.